﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Xml;

//自定义导出API的类型列表
public class CustomExportAPI
{ 
   public static List<Type> CustomTypeList
    {
        get
        {
            //********************************ToLua********************************************//
            // List<Type> list = new List<Type>();
            // foreach (var vb in CustomSettings.customTypeList)
            //     list.Add(vb.type);
            // return list;
            //******************************************************SLua***************************************************//
            // UnityEngine
            Assembly assembly = Assembly.Load("UnityEngine");
            Type[] types = assembly.GetExportedTypes();

            List<string> uselist;
            List<string> noUseList;

            CustomExport.OnGetNoUseList(out noUseList);
            CustomExport.OnGetUseList(out uselist);

            List<Type> exports = new List<Type>();

            System.Action<Type> Add = (t) =>
            {
                if (!exports.Contains(t))
                {
                    exports.Add(t);
                }
            };
            foreach (Type t in types)
            {
                if (LuaCodeGen.filterType(t, noUseList, uselist))
                    Add(t);
            }

            // UnityEngine.UI  

            CustomExport.OnGetNoUseList(out noUseList);
            CustomExport.OnGetUseList(out uselist);

            assembly = Assembly.Load("UnityEngine.UI");
            types = assembly.GetExportedTypes();
            foreach (Type t in types)
            {
                if (LuaCodeGen.filterType(t, noUseList, uselist))
                {
                    Add(t);
                }
            }

            // Custom Class     
            HashSet<string> namespaces = CustomExport.OnAddCustomNamespace();
            try
            {
                // export plugin-dll
                assembly = Assembly.Load("Assembly-CSharp-firstpass");
                types = assembly.GetExportedTypes();
                foreach (Type t in types)
                {
                    if (t.IsDefined(typeof(CustomLuaClassAttribute), false) || namespaces.Contains(t.Namespace))
                    {
                        Add(t);
                    }
                }

                // export self-dll
                assembly = Assembly.Load("Assembly-CSharp");
                types = assembly.GetExportedTypes();
                foreach (Type t in types)
                {
                    if (t.IsDefined(typeof(CustomLuaClassAttribute), false) || namespaces.Contains(t.Namespace))
                    {
                        Add(t);
                    }
                }
            }
            catch (Exception) { }

            // Custom Dll 
            try
            {
                // export plugin-dll
                assembly = Assembly.Load("Assembly-CSharp-firstpass");
                types = assembly.GetExportedTypes();
                foreach (Type t in types)
                {
                    if (t.IsDefined(typeof(CustomDllLuaClassAttribute), false))
                    {
                        Add(t);
                    }
                }

                // export self-dll
                assembly = Assembly.Load("Assembly-CSharp");
                types = assembly.GetExportedTypes();
                foreach (Type t in types)
                {
                    if (t.IsDefined(typeof(CustomDllLuaClassAttribute), false))
                    {
                        Add(t);
                    }
                }
            }
            catch (Exception) { }

            // third dll
            List<Type> cust = new List<Type>();
            List<string> assemblyList = new List<string>();
            CustomExport.OnAddCustomAssembly(ref assemblyList);
            foreach (string assemblyItem in assemblyList)
            {
                assembly = Assembly.Load(assemblyItem);
                types = assembly.GetExportedTypes();
                foreach (Type t in types)
                {
                    cust.Add(t);
                }
            }
            if (cust.Count > 0)
            {
                List<Type> exports2 = new List<Type>();
                foreach (Type t in cust)
                {
                    if (!exports.Contains(t))
                        exports2.Add(t);
                }
            }

            return exports;
        }
    }
}
